

class DataSound {
    isMusc: boolean = false;        //是否是背景音乐
    clip: cc.AudioClip = null;    // 声音的cc.AudioClip资源,默认为null
    id: number = null;            // 声音的播放id,默认为null
}

class SoundMgr {
    public static readonly staticInstance: SoundMgr = new SoundMgr();

    private _musicVolume: number = 1;         //背景音乐音量0-1
    private _soundVolume: number = 1;     //游戏音乐音量0-1
    private _isInVideo = false //判断是否在看视频广告 用于切换后台后是否继续播放音乐
    
    set musicVolume(num:number){
        if(this._musicVolume == num){
            return
        }
        this._musicVolume = num;
        cc.audioEngine.setMusicVolume(num)
    }

    get musicVolume():number{
        return this._musicVolume;
    }

    set soundVolume(num:number){
        if(this._soundVolume == num){
            return
        }
        this._soundVolume = num;

        cc.audioEngine.setEffectsVolume(num)
    }

    get soundVolume(): number{
        return this._soundVolume;
    }

    initSound(){
        let bgm_storage = g.storageMgr.getItem("music_volume");
        if(bgm_storage){
            this._musicVolume = parseFloat(bgm_storage)
        }
        let effect_storage = g.storageMgr.getItem("effect_volume");
        if(effect_storage){
            this._soundVolume = parseFloat(effect_storage);
        }
    }

    /**
     * 播放背景音乐
     * @param fileName 
     * @param isLoop 
     */
    async playMusic(fileName: string, isLoop: boolean = true) {
        return
        this.stopMusic()
        let soundClip = await g.resManager.loadRes(`sound/${fileName}`, cc.AudioClip)
        cc.audioEngine.setMusicVolume(this._musicVolume)
        cc.audioEngine.playMusic(soundClip,isLoop)
    }

    stopMusic() {
        cc.audioEngine.stopMusic()
    }

    pauseMusic(bVideo = false) {
        return
        this._isInVideo = bVideo
        cc.audioEngine.pauseMusic();
    }

    resumeMusic(bVideo = false) {
        return
        if(bVideo){
            cc.audioEngine.resumeMusic();
        }else if(!this._isInVideo){
            cc.audioEngine.resumeMusic();
        }
    }
    
    async playSound(fileName: string, isLoop: boolean = false) {
        return
        if(this._soundVolume <= 0) return
        let soundClip = await g.resManager.loadRes(`sound/${fileName}`, cc.AudioClip)
        cc.audioEngine.playEffect(soundClip,isLoop)
    }

    stopSound() {
        cc.audioEngine.stopAllEffects()
    }

    pauseSound() {
        cc.audioEngine.pauseAllEffects();
    }

    resumeSound() {
        cc.audioEngine.resumeAllEffects();
    }
}